﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.MT3DMS.DomainLayer;

using System.Collections.Specialized;



using System.Collections.ObjectModel;

using System.Windows.Input;





namespace HA.ModFlow.Controls
{
   public class PCSSMixing:PCBase
    {
       public PCSSMixing(SourceSinkMixing ssmix)
       {

           this._ssMixing =(SourceSinkMixing) ssmix.Clone () ;

           _items = new ObservableCollection<PCMixing>();


           this._lstUsePrecious = new List<bool>();
           this._lstUsePrecious.AddRange(ssmix.IsUsePrecious);
           foreach (MixingItem item in this._ssMixing.SpMixings)
           {
               PCMixing mix = new PCMixing((MixingItem)item.Clone(),this);
               this._items.Add(mix);               
           }


           this._periodStep = new List<int>();
           for (int i = 0; i < ssmix.NPeriods; i++)
           {
               this._periodStep.Add(i + 1);
           }


           this._evtSpConc = this._ssMixing.EvtSpConc;

           this._rechargeSpConc = this._ssMixing.RechargeSpConc;

           if (this._ssMixing.CurrentSpecies.Count > 0)
           {
               this._currentSPIndex = 0;

           }
           else
           {
               this._currentSPIndex = -1;
           }

           this._editRechargeCommand = new SSMEditRechargeCommand(this);

           this._editEvTCommand = new SSMEditEVTCommand(this);


 
       }


       int _currentPeriod;

       public int CurrentPeriod
       {
           get { return _currentPeriod; }
           set { _currentPeriod = value;

           this.UsePrecious = this._ssMixing.IsUsePrecious[_currentPeriod];

           foreach (PCMixing item in this._items)
           {

               item.CurrentIndex = this._currentPeriod;

           }

           this.OnPropertyChanged("UsePreciousEnabled");

           this.OnPropertyChanged("UsePrecious");
            
           
           }
       }


       List<int> _periodStep;
       public List<int> PeriodStep
       {
           get { return _periodStep; }

       }

       List<bool> _lstUsePrecious;

       public List<bool> LstUsePrecious
       {
           get { return _lstUsePrecious; }
           set { _lstUsePrecious = value; }
       }
       public bool UsePreciousEnabled
       {
           get
           {
               if (this._currentPeriod == 0)
               {
                   return false;
               }
               else
               {
                   return true;
               }
           }

       }

       public bool UsePrecious
       {
           get
           {
               if (_lstUsePrecious.Count == 0)
                   return false;
               return this._lstUsePrecious[this._currentPeriod];
           }
           set
           {
               this._lstUsePrecious[this._currentPeriod] = value;
               this._ssMixing .IsUsePrecious[this._currentPeriod] = value;

               this.OnPropertyChanged("UsePrecious");
           }
       }

       SourceSinkMixing _ssMixing;

       public SourceSinkMixing SsMixing
       {
           get { return _ssMixing; }
           set { _ssMixing = value; }
       }

       ObservableCollection<PCMixing> _items;

       public ObservableCollection<PCMixing> Items
       {
           get { return _items; }
           set { _items = value; }
       }


       List<List<SPPolygonConc>> _evtSpConc;

       public List<List<SPPolygonConc>> EvtSpConc
       {
           get { return _evtSpConc; }

       }



       List<List<SPPolygonConc>> _rechargeSpConc;

       public List<List<SPPolygonConc>> RechargeSpConc
       {
           get { return _rechargeSpConc; }

       }

       int _currentSPIndex;

       public int CurrentSPIndex
       {
           get { return _currentSPIndex; }
           set { _currentSPIndex = value; }
       }

       SSMEditRechargeCommand _editRechargeCommand;

       public SSMEditRechargeCommand EditRechargeCommand
       {
           get { return _editRechargeCommand; }
           set { _editRechargeCommand = value; }
       }

       SSMEditEVTCommand _editEvTCommand;

       public SSMEditEVTCommand EditEvTCommand
       {
           get { return _editEvTCommand; }
           set { _editEvTCommand = value; }
       }


       public void CreateNew()
       {
           MixingItem mi = this._ssMixing.BuildNewMixingItem();

           PCMixing pc = new PCMixing(mi,this);

           this._items.Add(pc);


       }


       List<MixingItem> _output;

       public List<MixingItem> Output
       {
           get { return _output; }
           set { _output = value; }
       }


       public void Confirm()
       {

           this._output = new List<MixingItem>();



           foreach (PCMixing item in this._items)
           {


               item.Confirm();

               this._output.Add(item.Mi);

           }
       }
       /// <summary>
       /// 检查层行列的正确定性
       /// </summary>
       /// <param name="row"></param>
       /// <param name="col"></param>
       /// <param name="layer"></param>
       /// <param name="index"></param>
       /// <param name="isIndexId"></param>
       /// <returns></returns>
       public bool CheckInputIJKorIndex(int row, int col, int layer)
       {
          
           foreach (PCMixing item in this._items)
           {             
                   if (item.Mi.Row <= 0 || item.Mi.Row > row)//行
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.Mi.Row + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
                   if (item.Mi.Col <= 0 || item.Mi.Col > col)
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.Mi.Col + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
                   if (item.Mi.Layer <= 0 || item.Mi.Layer > layer)
                   {
                       System.Windows.MessageBox.Show("不存在第 " + item.Mi.Layer + " 层，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                       return false;
                   }
           }
           return true;
       }

    }


   public class PCMixing : PCBase
   {


     public  PCMixing(MixingItem mi,PCSSMixing parent)
       {
           this._mi = mi;

           this._parent = parent;



           this._speciesList = new List<List<double>>();


           int nperiod = mi.NPeriods;

           for (int i = 0; i < nperiod; i++)
           {

               List<double> lst = new List<double>();

               foreach (List<double> item in this._mi.SpDic.Values)
               {

                   lst.Add(item[i]);

               }


               _speciesList.Add(lst);
               
           }

          
         
 
       }



       PCSSMixing _parent;

       MixingItem _mi;

       public MixingItem Mi
       {
           get { return _mi; }
           set { _mi = value; }
       }



       int _currentIndex;

       public int CurrentIndex
       {
           get { return _currentIndex; }
           set { _currentIndex = value;


           this.OnPropertyChanged("SpeciesValue");

           }
       }




       List<List<double>> _speciesList;

       public List<List<double>> SpeciesList
       {
           get { return _speciesList; }
           set { _speciesList = value; }
       }

       public List<double> SpeciesValue
       {

           get {

               List<double> sp = this._speciesList[this._currentIndex];

               return sp;



              
           
           
           }
       }



       public void Confirm()
       {

           int i=0;

             foreach (string sp in this._parent.SsMixing.CurrentSpecies)
	            {

                    for (int j = 0; j < this._speciesList.Count; j++)
                    {
                        this._mi.SpDic[sp][j] = this._speciesList[j][i];
                            
                    }


                
                   i++;
		 
	            }

          
 
       }


   }

   public class SSMEditRechargeCommand : ICommand
   {

       public SSMEditRechargeCommand(PCSSMixing parent)
       {
           this._parent = parent;
 
       }

       PCSSMixing _parent;

       public bool CanExecute(object parameter)
       {
           return this._parent.SsMixing.IsUseRecharge;
       }

       public event EventHandler CanExecuteChanged;

       public void Execute(object parameter)
       {
           if (this._parent.CurrentSPIndex != -1)
           {
               List<SPPolygonConc> data = this._parent.RechargeSpConc[this._parent.CurrentSPIndex];

               MT3DMSWindows.winPolygonConc win = new MT3DMSWindows.winPolygonConc(data);
               if (win.ShowDialog().Value)
               {
                   this._parent.RechargeSpConc[this._parent.CurrentSPIndex] = win.ArrySPPolygonConc;
               }
           }

          
          
       }
   }


   public class SSMEditEVTCommand : ICommand
   {
       public SSMEditEVTCommand(PCSSMixing parent)
       {
           this._parent = parent;
 
       }

       PCSSMixing _parent;

       public bool CanExecute(object parameter)
       {
           return this._parent.SsMixing.IsUseEvt;
       }

       public event EventHandler CanExecuteChanged;

       public void Execute(object parameter)
       {

           if (this._parent.CurrentSPIndex != -1)
           {
               List<SPPolygonConc> data = this._parent.EvtSpConc[this._parent.CurrentSPIndex];

               MT3DMSWindows.winPolygonConc win = new MT3DMSWindows.winPolygonConc(data);
               if (win.ShowDialog().Value)
               {
                   this._parent.EvtSpConc[this._parent.CurrentSPIndex] = win.ArrySPPolygonConc;
               }
           }

       }
   }



}
